package com.aimao.blog.config;

import com.aimao.blog.constant.ServerResponse;
import com.aimao.blog.util.TCException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author Jack Chen
 * @Create 2019-01-29 16:48
 */
@RestControllerAdvice
public class ErrorAdvice {

    private Logger log = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ServerResponse jsonConvertError(HttpMessageNotReadableException e) {
        log.error("FastJson 格式转换出错 ",e);
        return ServerResponse.createFailure("JSON 格式有误");
    }

    @ExceptionHandler(TCException.class)
    public ServerResponse tcErrorHandler(TCException e) {
        log.error("程序抛出自定义异常 ", e);
        return ServerResponse.createFailure(e.getMessage());
    }

    @ExceptionHandler(Throwable.class)
    public ServerResponse rrException(Throwable t) {
        log.error("服务器500 ", t);
        return ServerResponse.createFailure("服务器繁忙,请稍后重试!");
    }

}
